<template>
  <div :class="$style.wrap">
    <Button type="primary" @click="clickHandle">分配坐席</Button>
    <el-dialog :visible.sync="visible" :close-on-click-modal="false" title="分配坐席" @opened="openHandle">
      <FormContainer :column="1">
        <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
          <el-form-item label="所属业务组">
            <SearchSelectPicker
              url="/call-center/api/v1/businessGroup/page"
              placeholder="请选择所属业务组"
              lable-name="groupName"
              :value.sync="groupId"
              @selectChange="
                () => {
                  formData.newCustomerServiceName = '';
                  formData.newCustomerService = '';
                }
              "
            ></SearchSelectPicker>
          </el-form-item>
          <el-form-item prop="newCustomerService" label="执行坐席">
            <!-- :params="{query:{groupNo:{value:groupOption.map(e=>e.groupNo),operator:'in'}},type:0}" -->
            <SearchSelectPicker
              ref="customerServiceSelect"
              url="/call-center/api/v1/staffInfo/page"
              :other-raw="{ groupIds: [groupId] }"
              placeholder="请选择执行坐席"
              value-name="accountNo"
              lable-name="staffName"
              :parent-id="groupId"
              :value.sync="formData.newCustomerService"
              :lable.sync="formData.newCustomerServiceName"
            ></SearchSelectPicker>
          </el-form-item>
          <el-form-item>
            <Button type="primary" @click="confirmHandle">确认</Button>
            <Button type="primary" @click="visible=false">取消</Button>
          </el-form-item>
        </el-form>
      </FormContainer>
    </el-dialog>
  </div>
</template>
<script>
import SearchSelectPicker from '@/components/SearchSelectPicker'
import FormContainer from '@/components/FormContainer'
import { callBackTaskAllocation } from '@/api/callBackTask'
export default {
  name: 'Seats',
  components: {
    SearchSelectPicker,
    FormContainer
  },
  props: {
    selectedArry: {
      type: Array,
      default: () => []
    },
    objectMethod: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      groupId: '',
      visible: false,
      formData: {
        newCustomerService: '',
        newCustomerServiceName: ''
      },
      formRules: {
        newCustomerService: [{ required: true, message: '请选择执行坐席', trigger: 'change' }]
      }
    }
  },
  methods: {
    clickHandle() {
      if (this.selectedArry.length <= 0) {
        this.$message.info('请选择任务！')
        return
      }
      const enable = [6, '6']
      if (this.selectedArry.some(e => enable.includes(e.state))) {
        this.$message.info('不能选择回访成功的任务！')
        return
      }
      this.visible = true
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const data = {
          allocationType: 2,
          objectMethod: this.objectMethod
        }
        const callBackData = []
        for (const item of this.selectedArry) {
          callBackData.push({
            customerService: this.formData.newCustomerService,
            customerServiceName: this.formData.newCustomerServiceName,
            id: item.id,
            taskId: item.taskId
          })
        }
        // 0导入客户1线上订单2线下订单
        if (this.objectMethod === 0 || this.objectMethod === '0') {
          data.callBackObjectImportDTOList = callBackData
        } else if (this.objectMethod === 1 || this.objectMethod === '1') {
          data.callBackObjectOnlineDTOList = callBackData
        } else if (this.objectMethod === 2 || this.objectMethod === '2') {
          data.callBackObjectOfflineDTOList = callBackData
        }
        return callBackTaskAllocation(data).then(() => {
          this.$message.success('操作成功！')
          this.$emit('refresh')
          this.visible = false
        })
      })
    },
    openHandle() {
      this.$refs.form.resetFields()
      this.groupId = ''
    }
  }
}
</script>
<style lang='scss' module>
.wrap {
}
</style>
